<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改鉴定受理登记')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: summernote-css" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
    <th:block th:include="include :: bootstrap-select-css" />

</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-register-edit" th:object="${idAcceptRegister}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label is-required">送鉴单位（部门）：</label>
                        <div class="col-sm-8">
                            <input type="hidden"    th:field="*{sendUnitId}" class="form-control" placeholder="" readonly required>
                            <input type="text" name="sendUnit" id="sendUnit"  th:field="*{sendUnit}" class="form-control" placeholder="" required  autocomplete="off">
                            <span class="help-block m-b-none"><i class="fa fa-info-circle"></i>输入送鉴单位名称，自动补全</span>

                        </div>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label is-required">来文标题：</label>
                        <div class="col-sm-8">
                            <input name="title" class="form-control" th:field="*{title}" type="text" required>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label is-required">来文文号：</label>
                        <div class="col-sm-8">
                            <input name="documentNum" class="form-control " th:field="*{documentNum}" type="text" required>
                        </div>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">送鉴日期：</label>
                        <div class="col-sm-8">
                            <div class="input-group date">
                                <input name="sendDate" class="form-control" th:value="${#dates.format(idAcceptRegister.sendDate, 'yyyy-MM-dd')}" placeholder="yyyy-MM-dd" type="text">
                                <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">受理日期：</label>
                        <div class="col-sm-8">
                            <div class="input-group date">
                                <input name="acceptDate" class="form-control" th:value="${#dates.format(idAcceptRegister.acceptDate, 'yyyy-MM-dd')}" placeholder="yyyy-MM-dd" type="text">
                                <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-md-6">
                    <!--<div class="form-group">
                        <label class="col-sm-4 control-label is-required">鉴定组长：</label>
                        <div class="col-sm-8">
                            <input name="groupId" th:field="*{groupId}" class="form-control" type=hidden>
                            <select name="groupLeader" id="groupLeader" class="form-control" required>
                            </select>
                        </div>
                    </div>-->
                </div>
            </div>
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label is-required" >样本种类数量：</label>
                        <div class="col-sm-8">
                            <input name="types" class="form-control" th:field="*{types}" type="text" required>
                        </div>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                        <label class="col-sm-4 control-label is-required">样本数量：</label>
                        <div class="col-sm-8">
                            <input name="num" class="form-control digits" th:field="*{num}" type="text"  required number>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <div class="form-group">
                        <label class="col-sm-2 control-label">鉴定内容：</label>
                        <div class="col-sm-10">
                            <textarea name="identContent"  maxlength="2000"  rows="10" class="form-control">[[*{identContent}]]</textarea>

<!--                            <input type="hidden" class="form-control" th:field="*{identContent}" name="identContent">-->
<!--                            <div class="summernote" id="identContent"></div>-->
                        </div>
                    </div>
                </div>
            </div>
<!--            <div class="row">-->
<!--                <div class="col-md-12">-->
<!--                    <div class="form-group">-->
<!--                        <label class="col-sm-2 control-label">样本类别：</label>-->
<!--                        <div class="col-sm-10">-->
<!--                            <label th:each="category:${listCategory}" class="check-box">-->
<!--                                <input name="category" type="checkbox" th:value="${category.val}" th:text="${category.categoryName}" th:checked="${category.flag}" >-->
<!--                            </label>-->
<!--                        </div>-->
<!--                    </div>-->
<!--                </div>-->
<!--            </div>-->
            <div class="row">
                <div class="col-md-12">
                    <div class="form-group">
                        <label class="col-sm-2 control-label is-required">样本类别：</label>
                        <div class="col-sm-6">
                            <select name="category"  class="form-control noselect2 selectpicker" th:with="type=${@dict.getType('id_sample_type')}" data-none-selected-text="请选择审读范围" required multiple>
                                <option th:each="dict : ${type}" th:selected="(${#strings.contains(idAcceptRegister.category,dict.dictValue)})" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"  selected="selected"  > </option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <div class="form-group">
                        <label class="col-sm-2 control-label">来函文件：</label>
                        <div class="col-sm-10">
                            <input type="hidden" name="acceptFiles" th:value="*{acceptFiles}">
                            <div class="file-loading">
                                <input class="form-control file-upload" multiple id="acceptFiles" name="file" type="file">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-sm-12">
                    <div class="form-group">
                        <label class="col-sm-2 control-label"></label>
                        <div class="col-sm-10">
                            <button type="button" class="btn btn-w-m btn-warning" onclick="photograph('acceptFiles')">
                                <i class="fa fa-check"></i>拍照
                            </button>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-offset-5 col-sm-10">
                    <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i
                            class="fa fa-check"></i>保 存
                    </button>&nbsp;
                    <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i
                            class="fa fa-reply-all"></i>关 闭
                    </button>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: summernote-js" />
    <th:block th:include="include :: bootstrap-fileinput-js" />
    <th:block th:include="include :: bootstrap-select-js" />
    <th:block th:include="include :: bootstrap-typeahead-js"/>
    <!--模板JS引入-->
    <script th:src="@{/ajax/libs/template/template.js}"></script>
    <!--拍照预览图片模板-->
    <script type="text/html" id="template">
        <div class="file-preview-frame krajee-default  kv-preview-thumb file-preview-success"
             id={{id}} data-fileindex="0" data-fileid={{title}} data-template="image">
            <div class="kv-file-content">
                <img src={{src}} class="file-preview-image kv-preview-data" title={{title}}
                     style="width:213px;height:160px;" alt={{title}}>
            </div>
            <div class="file-thumbnail-footer">
                <div class="file-footer-caption" title={{title}}>
                    <div class="file-caption-info">{{title}}</div>
                    <div class="file-size-info"><samp>(108.21 KB)</samp></div>
                </div>
                <div class="file-thumb-progress kv-hidden" style="display: block;">
                    <div class="progress">
                        <div class="progress-bar bg-success progress-bar-success" role="progressbar" aria-valuenow="100"
                             aria-valuemin="0" aria-valuemax="100" style="width:100%;">
                            完成
                        </div>
                    </div>
                </div>
                <div class="file-upload-indicator" title="上传"><i class="glyphicon glyphicon-ok-sign text-success"></i></div>
                <div class="file-actions">
                    <div class="file-footer-buttons">
                        <button type="button" class="kv-file-upload btn btn-sm btn-kv btn-default btn-outline-secondary"
                                title="上传文件" style="display: none;"><i class="glyphicon glyphicon-upload"></i></button>
                        <button type="button" class="kv-file-remove btn btn-sm btn-kv btn-default btn-outline-secondary"
                                title="删除文件"><i class="glyphicon glyphicon-trash"></i></button>
                        <button type="button" class="kv-file-down btn btn-sm btn-default" title="下载附件"><i
                                class="fa fa-cloud-download"></i></button>
                    </div>
                </div>

                <div class="clearfix"></div>
            </div>
        </div>
    </script>

    <script th:inline="javascript">
        var prefix = ctx + "publish/register";
        $("#form-register-edit").validate({
            focusCleanup: true
        });

        /*上传参数配置 name 属性值必须为file*/
        var fileConfig = {
            uploadUrl: '/common/upload',
            maxFileSize: 30000,
            browseClass: 'btn btn-primary',
            dropZoneTitle: "拖拽文件到这里",
        }
        /*循环加载页面上传组件*/
        $.each($(".file-upload"), function () {
            var nodeId = $(this).attr('id');
            $.common.uploadFileInput(fileConfig, $(this), "input[name='" + nodeId + "']");
        });

        /*高拍仪调用*/
        function photograph(parameter) {
            $.modal.open("拍照", ctx + "publish/identsample/indexOne?parameter=" + parameter, 1200);
        }
        /*高拍仪回调*/
        function photo(_base64, _parameter, arr) {
            console.log(arr);
            $.operate.post('/common/uploadBase64',
                {"base64": _base64},
                function (result) {
                    if (result.code == web_status.SUCCESS) {
                        if ($("input[name='" + _parameter + "']").val() == null || $("input[name='" + _parameter + "']").val() == "") {
                            $("input[name='" + _parameter + "']").val(result.fileName);
                        } else {
                            $("input[name='" + _parameter + "']").val($("input[name='" + _parameter + "']").val() + ',' + result.fileName);
                        }
                        var id = new Date().getTime();
                        var title = result.fileName.substring(result.fileName.lastIndexOf("/") + 1);
                        var data = {
                            src: result.fileName,
                            arr: '0',
                            title: title,
                            id: id,
                        };

                        // 将数据放入模板中
                        var res = template("template", data);
                        $("input[name='" + _parameter + "']").next().find(".file-drop-zone-title").remove();
                        // 将模板放入页面中
                        $("input[name='" + _parameter + "']").next().find(".file-preview-thumbnails").append(res);
                        $("#" + _parameter).fileinput('_initFileActions');
                    } else {
                        $.modal.alertError(result.msg);
                    }
                }
            );

        }

        /*重新加载上传组件*/
        function reloadFileInput(_parameter) {
            var fileConfig = {
                uploadUrl: '/common/upload',
                maxFileSize: 30000,
                browseClass: 'btn btn-primary',
                dropZoneTitle: "拖拽文件到这里",
            }
            $.common.uploadFileInput(fileConfig, ("#" + _parameter), 'input[name="' + _parameter + '"]');
        }

        //获取鉴定组长信息
        function findheadman(){
            var userId = $("#groupId").val();
            $.ajax({
                type : 'get',
                url : ctx + "system/user/headmanList/12",
                dataType : "json",
                async : true, //是否异步请求
                success : function(datas) {
                    var str = "<option value=\"\" userid=\"\">--请选择鉴定组长--</option>";
                    for (var i = 0;i<datas.rows.length;i++){
                        if (userId==datas.rows[i].userId){
                            str +="<option value='"+datas.rows[i].userName+"'  userid='"+datas.rows[i].userId+"' selected=\"selected\">"+datas.rows[i].userName+"</option>";

                        }else{
                            str +="<option value='"+datas.rows[i].userName+"'   userid='"+datas.rows[i].userId+"'>"+datas.rows[i].userName+"</option>";

                        }
                    }
                    $("#groupLeader").append(str);
                }
            });
        }
        findheadman();

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.saveTab(prefix + "/edit", $('#form-register-edit').serialize());
            }
        }

        $("input[name='sendDate']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("input[name='acceptDate']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $(function() {
            $('.summernote').each(function(i) {
                $('#' + this.id).summernote({
                    lang: 'zh-CN',
                    callbacks: {
                        onChange: function(contents, $edittable) {
                            $("input[name='" + this.id + "']").val(contents);
                        },
                        onImageUpload: function(files) {
							var obj = this;
							var data = new FormData();
							data.append("file", files[0]);
							$.ajax({
								type: "post",
								url: ctx + "common/upload",
								data: data,
								cache: false,
								contentType: false,
								processData: false,
								dataType: 'json',
								success: function(result) {
									if (result.code == web_status.SUCCESS) {
										$('#' + obj.id).summernote('insertImage', result.url);
									} else {
										$.modal.alertError(result.msg);
									}
								},
								error: function(error) {
									$.modal.alertWarning("图片上传失败。");
								}
							});
						}
                    }
                });
                var content = $("input[name='" + this.id + "']").val();
                $('#' + this.id).summernote('code', content);
            })


            $("#groupLeader").change(function () {
                var selected = $(this).children('option:selected').attr("userid");
                if (selected){
                    $("#groupId").val(selected);
                    console.log("不为空"+selected)
                }else {
                    $("#groupId").val("");
                    console.log("为空"+selected)
                }

            });
        });

        /**
         * 其他按钮（如下载）绑定事件
         */
        $(document).on('click', '.kv-file-down', function (e, id, index) {
            var download = "";
            // 获取父级DIV
            debugger
            var fileIndex = $(this).parents(".file-preview-frame").attr("data-fileindex");
            var fileValue = $(this).parents(".col-sm-10").children("input").val();
            if(!isNaN(fileIndex)){
                var title = $(this).parents(".file-preview-frame").attr("title");
                var id = $(this).parents(".file-preview-frame").attr("id");
                if(title==""||title==null){
                    title = $("#"+id+" img").attr("title");
                    for(var i = 0 ;i<=fileValue.split(",").length-1;i++){
                        var title1 = fileValue.split(",")[i].substring(fileValue.split(",")[i].lastIndexOf("/")+1);
                        if(title1 == title ){
                            download =fileValue.split(",")[i];
                        }
                    }
                }else{
                    for(var i = 0 ;i<=fileValue.split(",").length-1;i++){
                        var title1 = fileValue.split(",")[i].substring(fileValue.split(",")[i].lastIndexOf("/")+1);
                        if(title1 == title ){
                            download =fileValue.split(",")[i];
                        }
                    }
                }
            }else {
                var number = fileIndex.match(/\d/);
                if (number) {
                    download = fileValue.split(",")[number];
                }
            }
            var name=download.substring(download.lastIndexOf("/")+1);
            if(download!=""){
                downloadFile(download,name);
            }
        })

        function downloadFile(url,name){
            var a = document.createElement("a");
            a.download = name;
            a.href = url;
            $("body").append(a); // 修复firefox中无法触发click
            a.click();
            $(a).remove();
        }

        // 送检单位自动补全  pageSize: 10  pageNum: 1
        $("#sendUnit").typeahead({
            source: function (query, process) {
                return $.ajax({
                    url: '/publish/enforcement/list',
                    type: 'post',
                    data: {companyName: query, pageSize: 10, pageNum: 1, isAsc: 'asc', orderByColumn:''},
                    success: function (result) {
                        var items =[];
                        var resultList = result.rows.map(function (item) {
                            var aItem = {id: item.id, name: item.departName};
                            items.push(aItem);
                        });
                        return process(items);
                    }
                });
            },
            afterSelect: function (item) {//选中一条数据后的回调函数，此处可以向隐藏域赋值数据id
                console.log(item);//取出选中项的值
                $("input[name='sendUnitId']").val(item.id);
            },
            delay: 800,
            fitToElement: true
        });
    </script>
</body>
</html>